Create Event
Description
The create_event
function sends a POST request to the web service to create a new event. It returns a WebServiceResult
object containing the response code, response string, and parsed JSON data.
Function Signature:
def create_event(ws_config: WebServiceConfig, device_id: int, device_serial: str, agent_type: str, event_type: str, server_event: int = 0, event_parameters: str = '', priority=1, max_attempts: int = 0, expiration_epoch: int = 0) -> WebServiceResult:
Parameters
- ws_config (WebServiceConfig): Configuration for the web service connection.
- device_id (int): The ID of the device for which to create the event.
- device_serial (str): The serial number of the device.
- agent_type (str): The type of the agent creating the event.
- event_type (str): The type of event to create.
- server_event (int, optional): A flag indicating whether this is a server event. Default is
0
. - event_parameters (str, optional): Additional parameters related to the event. Default is an empty string.
- priority (int, optional): The priority of the event. Default is
1
. - max_attempts (int, optional): The maximum number of attempts to create the event. Default is
0
. - expiration_epoch (int, optional): The expiration time for the event, represented as a Unix epoch. Default is
0
.
Returns
- WebServiceResult: A result object containing:
http_response_code
: The response code from the web service.http_response_string
: The response string from the web service.json_data
: The parsed JSON response from the web service, orNone
if there was an error.code
: A status code (0 for success, -1 for failure).description
: A description of any error that occurred.
Example Usage
ws_config = WebServiceConfig(base_url="https://api.actionstreamer.com")
device_id = 123
device_serial = "ABC123"
agent_type = "sensor"
event_type = "temperature"
result = create_event(ws_config, device_id, device_serial, agent_type, event_type)
print(result.http_response_code)
print(result.json_data)
Behavior
- The function sends a POST request to the web service to create an event with the provided parameters.
- The response is parsed into a
WebServiceResult
object, which contains the response code, response string, and parsed JSON data. - If an error occurs, an exception is caught and the error details are included in the result.
Error Handling
- General Exception: Any errors during the request or JSON processing will be caught, and the error description will be included in the result.
- Exception Information: The function prints the filename and line number for easier debugging when an exception is encountered.